package com.blog.cmrpersonalblog.dto.article.request;

import com.blog.cmrpersonalblog.common.BasePageRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;

import jakarta.validation.constraints.NotNull;

/**
 * 文章评论列表查询请求
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ArticleCommentListRequest extends BasePageRequest {

    /**
     * 文章ID
     */
    @NotNull(message = "文章ID不能为空")
    private Long articleId;

    /**
     * 根评论ID（可选，用于获取特定评论的回复）
     */
    private Long rootId;

    /**
     * 是否只获取顶级评论
     */
    private Boolean topLevelOnly = true;

    /**
     * 排序方式：latest-最新, hot-热门, oldest-最早
     */
    private String sortType = "latest";

    /**
     * 构造函数
     */
    public ArticleCommentListRequest() {
        // 设置默认排序字段
        setSortField("create_time");
        setSortOrder("desc");
    }

    protected boolean isValidSortField(String field) {
        return field != null && (
            "create_time".equals(field) ||
            "like_count".equals(field) ||
            "reply_count".equals(field) ||
            "id".equals(field)
        );
    }

    /**
     * 根据排序类型设置排序字段
     */
    public void applySortType() {
        switch (sortType) {
            case "hot":
                setSortField("like_count");
                setSortOrder("desc");
                break;
            case "oldest":
                setSortField("create_time");
                setSortOrder("asc");
                break;
            case "latest":
            default:
                setSortField("create_time");
                setSortOrder("desc");
                break;
        }
    }
}
